package kevin.hbase.common;

import lombok.Data;

@Data
public class HttpResult<T> {
    boolean success;
    int code;
    String msg;
    T data;
    long timestamp = System.currentTimeMillis();

    public static HttpResult success() {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.SUCCESS.success);
        result.setCode(Status.SUCCESS.code);
        result.setMsg(Status.SUCCESS.msg);
        return result;
    }

    public static HttpResult success(Object data) {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.SUCCESS.success);
        result.setCode(Status.SUCCESS.code);
        result.setMsg(Status.SUCCESS.msg);
        result.setData(data);
        return result;
    }

    public static HttpResult failed() {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.ERROR.success);
        result.setCode(Status.ERROR.code);
        result.setMsg(Status.ERROR.msg);
        return result;
    }

    public static HttpResult failed(String msg) {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.OTHERS_ERROR.success);
        result.setCode(Status.OTHERS_ERROR.code);
        result.setMsg(msg);
        return result;
    }

    public static HttpResult failed(Status status) {
        return failed(status.code, status.msg);
    }

    public static HttpResult failed(int code, String msg) {
        HttpResult result = new HttpResult();
        result.setSuccess(false);
        result.setCode(code);
        result.setMsg(msg);
        return result;
    }

    public static HttpResult requiredParamBlank() {
        HttpResult result = new HttpResult();
        result.setSuccess(Status.REQUIRED_PARAM_BLANK.success);
        result.setCode(Status.REQUIRED_PARAM_BLANK.code);
        result.setMsg(Status.REQUIRED_PARAM_BLANK.msg);
        return result;
    }

    public enum Status {
        SUCCESS(true, 200, "操作成功"),
        REQUIRED_PARAM_BLANK(false, 301, "必填参数为空"),
        ERROR(false, 302, "系统异常"),
        TOKEN_IS_EXPIRED(false, 303, "token已过期"),
        DEVICE_IS_BINDED(false, 304, "设备已经被绑定"),
        DEVICE_NOT_EXISTED(false, 305, "设备不存在"),
        VEHICLE_NOT_EXISTED(false, 306, "车辆不存在"),
        PLATENO_IS_BINDED(false, 307, "车牌号已经被绑定"),
        VIN_IS_BINDED(false, 308, "车架号已经被绑定"),
        SIGN_AUTH_FAILED(false, 309, "验签失败"),
        WHITE_LIST_FAILED(false, 310, "白名单失败"),
        REQ_METHOD_ERROR(false, 311, "请求方式错误"),
        PARAMS_VALID_ERROR(false, 312, "参数格式验证失败"),
        TOKEN_IS_NULL(false, 313, "token为空"),
        TOKEN_VALID_FAILED(false, 314, "token校验失败"),

        TIMESTAMP_IS_NULL(false, 315, "时间戳为空"),
        REQUEST_URL_IS_EXPIRED(false, 316, "请求URL过期"),
        API_USER_NOT_EXISTED(false, 317, "appId不存在，请联系管理员"),
        TOKEN_VALUE_ERROR(false, 318, "token值错误"),
        API_USER_DISABLED(false, 319, "appId用户被禁用"),
        API_USER_EXPIRED(false, 320, "appId用户已过期"),
        OTHERS_ERROR(false, 999, "");


        public final boolean success;
        public final int code;
        public final String msg;

        Status(boolean success, int code, String msg) {
            this.success = success;
            this.code = code;
            this.msg = msg;
        }
    }

}
